<!DOCTYPE html>
<html lang="zh">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>智慧农业边缘云控一体化系统</title>
  <style>
    /* Reset basic styles */
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }

    body {
      font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
      background: linear-gradient(45deg, #2a2a72, #009ffd);
      color: #fff;
      line-height: 1.6;
    }

    header {
      text-align: center;
      padding: 30px;
      background: rgba(0, 0, 0, 0.6);
      border-bottom: 2px solid #fff;
    }

    header h1 {
      font-size: 2.5rem;
      margin-bottom: 15px;
      text-transform: uppercase;
      letter-spacing: 2px;
    }

    header a {
      color: #fff;
      text-decoration: none;
      font-size: 1rem;
      margin: 0 20px;
      padding: 10px 15px;
      border-radius: 30px;
      background-color: #009ffd;
      transition: background-color 0.3s ease, transform 0.3s ease;
    }

    header a:hover {
      background-color: #00b8d8;
      transform: scale(1.1);
    }

    .container {
      display: grid;
      grid-template-columns: repeat(4, 1fr); /* 每行 4 个容器 */
      gap: 20px;
      padding: 40px;
    }

    .section {
      background-color: rgba(0, 0, 0, 0.5);
      border-radius: 10px;
      padding: 20px;
      text-align: center;
      box-shadow: 0 4px 15px rgba(0, 0, 0, 0.3);
      transition: transform 0.3s ease, box-shadow 0.3s ease;
    }

    .section:hover {
      transform: translateY(-10px);
      box-shadow: 0 10px 20px rgba(0, 0, 0, 0.5);
    }

    .section h2 {
      font-size: 1.3rem;
      margin-bottom: 10px;
      color: #fff;
      letter-spacing: 1px;
    }

    .value {
      font-size: 2.2rem;
      font-weight: bold;
      color: #00d4ff;
    }

    .value.normal {
      color: #00ff00;
    }

    .value.alert {
      color: #ff5c5c;
    }

    footer {
      background-color: #000;
      color: #fff;
      text-align: center;
      padding: 15px;
      position: fixed;
      bottom: 0;
      width: 100%;
    }
  </style>
</head>

<body>
  <header>
    <h1>智慧农业边缘云控一体化系统</h1>
    <div>
      <body onload="init()">
        <a href="index.html">数据显示页面</a>
        <a href="control.html">控制页面</a>
        <a href="video.html">实时环境监控</a>
    </body>
    </div>
  </header>

  <div class="container">
    <!-- 环境数据展示区 -->
    <div class="section">
      <h2>环境温度</h2>
      <div class="value" id="temperature">0C</div>
    </div>

    <div class="section">
      <h2>环境湿度</h2>
      <div class="value" id="humidity">0%</div>
    </div>

    <div class="section">
      <h2>光照强度</h2>
      <div class="value" id="light">0Lux</div>
    </div>

    <div class="section">
      <h2>二氧化碳浓度</h2>
      <div class="value" id="co2">0%</div>
    </div>

    <div class="section">
      <h2>氧气浓度</h2>
      <div class="value" id="oxygen">0%</div>
    </div>

    <div class="section">
      <h2>土壤湿度</h2>
      <div class="value" id="soil">0%</div>
    </div>

    <div class="section">
      <h2>电量采集</h2>
      <div class="value" id="ele">0V</div>
    </div>

    <!-- 环境报警区 -->
    <div class="section">
      <h2>火焰报警</h2>
      <div class="value alert" id="flame">警报</div>
    </div>
  </div>


  <script>

    function init() {
      refresh();
    }

    function refresh() {
      var xhr = new XMLHttpRequest(); // 创建XMLHttpRequest对象
      var url = ""; // 请求的URL

      xhr.open("POST", url, true); // 配置请求：POST方法、URL、异步请求

      var post_req = {
        "type": 1,
        "limit": "all"
      };

      xhr.onreadystatechange = function () {
        if (xhr.readyState === 4 && xhr.status === 200) 
        { // 请求完成且成功
          var response = xhr.responseText; // 获取响应数据
          var info = JSON.parse(response);  //将json字符串反序列化为json对象
          console.log(info); // 在浏览器控制台打印响应数据
        
          document.getElementById('temperature').innerText = info.Tem.toFixed(2)+"°C";
          temperatureData=info.Tem;
          document.getElementById('humidity').innerText = info.Hum.toFixed(2)+"%";
          humidityData=info.Hum;
          document.getElementById('light').innerText = info.light+" Lux";
          brightnessData=info.light;
          document.getElementById('ele').innerText = info.ele.toFixed(2) + " v";
          eleData=info.ele;
          if(info.flame=="0")
          {
            document.getElementById('flame').innerText="正常";
          }
          else
          {
            document.getElementById('flame').innerText="警告!!有火情!!";
          }
       
          // document.getElementById('co2').innerText = info.co2+" ppm";
          document.getElementById('co2').innerText = info.co2.toFixed(2) + " %";
      
          // document.getElementById('oxygen').innerText = info.o2+"%";
          document.getElementById('oxygen').innerText = info.o2.toFixed(2) + " %";
  
          // document.getElementById('soil').innerText = info.soil+" %";
          document.getElementById('soil').innerText = info.soil.toFixed(2) + " %";
          
        }

      };
      //将json对象序列化为json字符串,序列化并发送请求
      xhr.send(JSON.stringify(post_req)); // 发送请求，并将数据转换为JSON字符串
    }
// 定时刷新数据
   setInterval(function () {
      init();
    }, 2000);
  </script>
</body>
</html>